//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;

namespace SoapTrace.Tests.TcpPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="TcpPacketMonitor"/> class
	/// registers two different clients connecting and then disconnecting.  
	/// The clients have the same IP address, but a different port.
	/// </summary>
	[ TestFixture ]
	public class TwoClientsDisconnectTestFixture
	{
		TcpPacketMonitor Monitor;
		TcpCommunicationEventArgs FirstClientEventArgsReceived;
		TcpCommunicationEventArgs SecondClientEventArgsReceived;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			FirstClientEventArgsReceived = null;
			SecondClientEventArgsReceived = null;
			Monitor = new TcpPacketMonitor( "209.171.52.99", 80 );
			Monitor.ClientDisconnected += new TcpCommunicationEventHandler( OnClientDisconnected );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				Monitor.ProcessPacket( Entry.Packet );
			}
		}
		
		/// <summary>
		/// Check the disconnection source IP address is correct.
		/// </summary>
		[ Test ]
		public void FirstClientIPAddress( )
		{
			Assert.AreEqual( "192.168.1.3:1357", FirstClientEventArgsReceived.SourceAddress.ToString( ),
			                "Unexpected client IP address." );
		}
		
		/// <summary>
		/// Check the disconnection destination IP address is correct.
		/// </summary>
		[ Test ]
		public void FirstServerIPAddress( )
		{
			Assert.AreEqual( "209.171.52.99:80", FirstClientEventArgsReceived.DestinationAddress.ToString( ),
			                "Unexpected server IP address." );
		}		
		
		/// <summary>
		/// Check the disconnection source IP address is correct.
		/// </summary>
		[ Test ]
		public void SecondClientIPAddress( )
		{
			Assert.AreEqual( "192.168.1.3:7001", SecondClientEventArgsReceived.SourceAddress.ToString( ),
			                "Unexpected client IP address." );
		}
		
		/// <summary>
		/// Check the disconnection destination IP address is correct.
		/// </summary>
		[ Test ]
		public void SecondServerIPAddress( )
		{
			Assert.AreEqual( "209.171.52.99:80", SecondClientEventArgsReceived.DestinationAddress.ToString( ),
			                "Unexpected server IP address." );
		}			
		
		private void OnClientDisconnected( object sender, TcpCommunicationEventArgs e )
		{
			if( e.SourceAddress.ToString( ) == "192.168.1.3:1357" )
			{
				FirstClientEventArgsReceived = e;
			}
			else
			{
				SecondClientEventArgsReceived = e;
			}
		}
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
				
				// First client connection.
				
				"  <Packet Time=\"2005-02-03 14:38:03.39\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 2A 24 40 00 80 06 08 EA C0 A8 01 03 D1 AB 34 63 05 4D 00 50 47 10 E7 88 00 00 00 00 70 02 FF FF 87 2F 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:03.44\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 86 D0 40 00 66 06 C6 3D D1 AB 34 63 C0 A8 01 03 00 50 05 4D 59 89 85 1D 47 10 E7 89 70 12 FF FF A8 9F 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:03.49\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 25 40 00 80 06 08 F1 C0 A8 01 03 D1 AB 34 63 05 4D 00 50 47 10 E7 89 59 89 85 1E 50 10 FF FF D5 3B 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				
				// Second client connection.

				"  <Packet Time=\"2005-02-03 14:38:04.39\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 2A 24 40 00 80 06 08 EA C0 A8 01 03 D1 AB 34 63 1B 59 00 50 47 10 E7 88 00 00 00 00 70 02 FF FF 87 2F 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:04.44\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 86 D0 40 00 66 06 C6 3D D1 AB 34 63 C0 A8 01 03 00 50 1B 59 59 89 85 1D 47 10 E7 89 70 12 FF FF A8 9F 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:04.49\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 25 40 00 80 06 08 F1 C0 A8 01 03 D1 AB 34 63 1B 59 00 50 47 10 E7 89 59 89 85 1E 50 10 FF FF D5 3B 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				
				// First client disconnection.
				
				"  <Packet Time=\"2005-02-03 14:38:05.69\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 26 40 00 80 06 08 F0 C0 A8 01 03 D1 AB 34 63 05 4D 00 50 47 10 E7 89 59 89 85 1E 50 11 FF FF D5 3A 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:06.24\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 A4 A5 40 00 66 06 A8 70 D1 AB 34 63 C0 A8 01 03 00 50 05 4D 59 89 85 1E 47 10 E7 8A 50 10 FF FF D5 3A 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:06.29\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 A4 A6 40 00 66 06 A8 6F D1 AB 34 63 C0 A8 01 03 00 50 05 4D 59 89 85 1E 47 10 E7 8A 50 11 FF FF D5 39 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:06.34\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 27 40 00 80 06 08 EF C0 A8 01 03 D1 AB 34 63 05 4D 00 50 47 10 E7 8A 59 89 85 1F 50 10 FF FF D5 39 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				
				// Second client disconnection.
				
				"  <Packet Time=\"2005-02-03 14:38:06.69\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 26 40 00 80 06 08 F0 C0 A8 01 03 D1 AB 34 63 1B 59 00 50 47 10 E7 89 59 89 85 1E 50 11 FF FF D5 3A 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:07.24\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 A4 A5 40 00 66 06 A8 70 D1 AB 34 63 C0 A8 01 03 00 50 1B 59 59 89 85 1E 47 10 E7 8A 50 10 FF FF D5 3A 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:07.29\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 A4 A6 40 00 66 06 A8 6F D1 AB 34 63 C0 A8 01 03 00 50 1B 59 59 89 85 1E 47 10 E7 8A 50 11 FF FF D5 39 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 14:38:07.34\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 2A 27 40 00 80 06 08 EF C0 A8 01 03 D1 AB 34 63 1B 59 00 50 47 10 E7 8A 59 89 85 1F 50 10 FF FF D5 39 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +			
				"</Packets>";
		}
	}
}
